//
//  MoreFindPeopleListViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/5/17.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "MoreFindPeopleListViewController.h"
#import "SearchWorkmateCell.h"
#import "IncumbentModel.h"
#import "WorkmateMainVC.h"

#import "MoreWorkmateCell.h"
#import "WorkmateModel.h"

@interface MoreFindPeopleListViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MoreFindPeopleListViewController {
    NSInteger _workmatePage;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"更多同事";
    
//    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SearchWorkmateCell class]) bundle:nil] forCellReuseIdentifier:@"searchWorkmateCellMorePeople"];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MoreWorkmateCell class]) bundle:nil] forCellReuseIdentifier:@"MoreWorkmateCellFindPeople"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _workmatePage = 1;
        [self loadData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _workmatePage += 1;
        [self loadData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadData {
    WS(weakSelf)
    [SendRequest getMatchingIncumbentsWithPageNo:[NSString stringWithFormat:@"%zd", _workmatePage] result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            SaintiLog(@"谁能帮我 = %@", [result objectForKey:@"data"]);
            NSArray *datas = [[result objectForKey:@"data"] objectForKey:@"list"];
            NSArray *workmates = [IncumbentModel mj_objectArrayWithKeyValuesArray:datas];
            
            if (_workmatePage == 1) {
                [weakSelf.dataArray removeAllObjects];
                [weakSelf.dataArray addObjectsFromArray:workmates];
                
                [weakSelf.tableView.mj_header endRefreshing];
                [weakSelf.tableView.mj_footer endRefreshing];
            } else {
                [weakSelf.dataArray addObjectsFromArray:workmates];
                if (workmates.count == kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshing];
                } else {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
            [weakSelf.tableView reloadData];
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
            if (_workmatePage == 1) {
                [weakSelf.tableView.mj_header endRefreshing];
            } else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
        }
    }];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    SearchWorkmateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchWorkmateCellMorePeople"];
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.incumbentModel = [self.dataArray objectAtIndex:indexPath.row];
    
    MoreWorkmateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreWorkmateCellFindPeople"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    cell.incumbentModel = [self.dataArray objectAtIndex:indexPath.row];
    
    IncumbentModel *incumbent = [self.dataArray objectAtIndex:indexPath.row];
    WorkmateModel *model = [[WorkmateModel alloc] init];
    model.headImg = incumbent.headImg;
    model.nickName = incumbent.nickName;
    model.hasAssurePassService = incumbent.hasAssurePassService;
    model.positionName = incumbent.positionName;
    model.inServiceTime = incumbent.inServiceTime;
    model.helpedCount = incumbent.helpedCount;
    model.degreeOfSatisfaction = incumbent.degreeOfSatisfaction;
    
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 104;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    IncumbentModel *model = [self.dataArray objectAtIndex:indexPath.row];
    WorkmateMainVC *vc = [[WorkmateMainVC alloc] init];
    vc.userId = model.userId;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
